﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using MongoDB.Driver;

namespace MongoCamp.Linq
{
    public abstract class MongoQueryBase<TEntityClass> : IQueryable<TEntityClass>
    {
        protected string CollectionName;
        protected MongoServerPack MongoServer { get; set; }

        protected MongoCursor<TEntityClass> MongoCursor;
        protected Expression _Expression;

        public static readonly IQueryProvider DefaultProvider = new MongoQueryProvider();

        protected MongoQueryBase(string collectionName, MongoServerPack mongoServer)
        {
            CollectionName = collectionName;
            MongoServer = mongoServer;
        }

        protected MongoQueryBase(string collectionName)
            : this(collectionName, MongoServerPack.DefaultDB)
        { }


        public IEnumerator<TEntityClass> GetEnumerator()
        {
            return (IEnumerator<TEntityClass>)Provider.Execute<TEntityClass>(_Expression);
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return (IEnumerator)Provider.Execute(_Expression);
        }

        public Type ElementType
        {
            get { return typeof(TEntityClass); }
        }

        public Expression Expression
        {
            get { return _Expression; }
        }

        public IQueryProvider Provider
        {
            get { return DefaultProvider; }
        }
    }
}